import pandas as pd
import plotly.express as px
import plotly.graph_objs as go
from plotly.subplots import make_subplots
from urllib.request import urlopen
import json
import requests
import re
import math
import warnings
warnings.filterwarnings('ignore')
confirmed_color = 'navy'
recovered_color = 'green'
death_color = 'indianred'
active_color = 'purple'
df1 = "https://api.covid19india.org/state_district_wise.json"
df2 = "https://api.covid19india.org/data.json"
def getting_data(url):
response = requests.get(url)
data = response.content.decode('utf-8')
return data
df_state = json.loads(getting_data(df1))
df = json.loads(getting_data(df2))
lis = []
state_names = df_state.keys()
for state in state_names:
district_names = df_state[state]['districtData'].keys() #Districts of Current State
for district in district_names:
temp = df_state[state]['districtData'][district]
var_lis = [state,district,temp.get('confirmed'),temp.get('recovered'),
temp.get('active'),temp.get('deceased')]
lis.append(var_lis)
district_wise = pd.DataFrame(lis,columns=['State/UT','District','Confirmed',
'Recovered','Active','Death'])
district_wise.head()
| State/UT | District | Confirmed | Recovered | Active | Death | |
|---|---|---|---|---|---|---|
| 0 | State Unassigned | Unassigned | 0 | 0 | 0 | 0 |
| 1 | Andaman and Nicobar Islands | Nicobars | 0 | 0 | 0 | 0 |
| 2 | Andaman and Nicobar Islands | North and Middle Andaman | 1 | 1 | 0 | 0 |
| 3 | Andaman and Nicobar Islands | South Andaman | 51 | 32 | 19 | 0 |
| 4 | Andaman and Nicobar Islands | Unknown | 7496 | 7380 | -13 | 129 |
temp = [[i['state'],i['confirmed'],i['recovered'],i['active'],i['deaths'],
i['lastupdatedtime']] for i in df['statewise']]
statewise_total = pd.DataFrame(temp,columns=['State/UT','Confirmed','Recovered',
'Active','Death','LastUpdateTime'])
statewise_total.head()
| State/UT | Confirmed | Recovered | Active | Death | LastUpdateTime | |
|---|---|---|---|---|---|---|
| 0 | Total | 32249900 | 31441260 | 363849 | 432112 | 13/08/2021 23:27:22 |
| 1 | Andaman and Nicobar Islands | 7549 | 7419 | 1 | 129 | 13/08/2021 23:27:22 |
| 2 | Andhra Pradesh | 1994606 | 1963728 | 17218 | 13660 | 13/08/2021 23:27:22 |
| 3 | Arunachal Pradesh | 51513 | 49425 | 1836 | 252 | 13/08/2021 23:27:22 |
| 4 | Assam | 580657 | 566101 | 7707 | 5502 | 13/08/2021 23:27:22 |
statewise_total['Confirmed']=statewise_total['Confirmed'].astype('int')
statewise_total['Recovered']=statewise_total['Recovered'].astype('int')
statewise_total['Active']=statewise_total['Active'].astype('int')
statewise_total['Death']=statewise_total['Death'].astype('int')
statewise_total['RecoveryRate%'] = round(statewise_total['Recovered']/statewise_total['Confirmed']*100,2)
statewise_total['MortalityRate%'] = round(statewise_total['Death']/statewise_total['Confirmed']*100,2)
statewise_total['Active/100 Confirmed'] = round(statewise_total['Active']/statewise_total['Confirmed']*100,2)
for i,y in enumerate(statewise_total['LastUpdateTime']):
statewise_total['LastUpdateTime'][i] = pd.to_datetime(y.split(' ')[0])
statewise_total['LastUpdateTime'] = pd.to_datetime(statewise_total['LastUpdateTime'])
statewise_total.head()
| State/UT | Confirmed | Recovered | Active | Death | LastUpdateTime | RecoveryRate% | MortalityRate% | Active/100 Confirmed | |
|---|---|---|---|---|---|---|---|---|---|
| 0 | Total | 32249900 | 31441260 | 363849 | 432112 | 2021-08-13 | 97.49 | 1.34 | 1.13 |
| 1 | Andaman and Nicobar Islands | 7549 | 7419 | 1 | 129 | 2021-08-13 | 98.28 | 1.71 | 0.01 |
| 2 | Andhra Pradesh | 1994606 | 1963728 | 17218 | 13660 | 2021-08-13 | 98.45 | 0.68 | 0.86 |
| 3 | Arunachal Pradesh | 51513 | 49425 | 1836 | 252 | 2021-08-13 | 95.95 | 0.49 | 3.56 |
| 4 | Assam | 580657 | 566101 | 7707 | 5502 | 2021-08-13 | 97.49 | 0.95 | 1.33 |
timeseries = [list(i.values()) for i in df['cases_time_series']]
timeseries = pd.DataFrame(timeseries,columns=df['cases_time_series'][0].keys())
timeseries.head()
| dailyconfirmed | dailydeceased | dailyrecovered | date | dateymd | totalconfirmed | totaldeceased | totalrecovered | |
|---|---|---|---|---|---|---|---|---|
| 0 | 1 | 0 | 0 | 30 January 2020 | 2020-01-30 | 1 | 0 | 0 |
| 1 | 0 | 0 | 0 | 31 January 2020 | 2020-01-31 | 1 | 0 | 0 |
| 2 | 0 | 0 | 0 | 1 February 2020 | 2020-02-01 | 1 | 0 | 0 |
| 3 | 1 | 0 | 0 | 2 February 2020 | 2020-02-02 | 2 | 0 | 0 |
| 4 | 1 | 0 | 0 | 3 February 2020 | 2020-02-03 | 3 | 0 | 0 |
timeseries['dailyconfirmed'] = timeseries['dailyconfirmed'].astype('int')
timeseries['dailydeceased'] = timeseries['dailydeceased'].astype('int')
timeseries['dailyrecovered'] = timeseries['dailyrecovered'].astype('int')
timeseries['totalconfirmed'] = timeseries['totalconfirmed'].astype('int')
timeseries['totaldeceased'] = timeseries['totaldeceased'].astype('int')
timeseries['totalrecovered'] = timeseries['totalrecovered'].astype('int')
timeseries['7dyMnConfirmed'] = timeseries.totalconfirmed.rolling(7).mean().fillna(0).astype(int)
timeseries['7dyMnRecovered'] = timeseries.totalrecovered.rolling(7).mean().fillna(0).astype(int)
timeseries['7dyMnDeceased'] = timeseries.totaldeceased.rolling(7).mean().fillna(0).astype(int)
timeseries.head(10)
| dailyconfirmed | dailydeceased | dailyrecovered | date | dateymd | totalconfirmed | totaldeceased | totalrecovered | 7dyMnConfirmed | 7dyMnRecovered | 7dyMnDeceased | |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 1 | 0 | 0 | 30 January 2020 | 2020-01-30 | 1 | 0 | 0 | 0 | 0 | 0 |
| 1 | 0 | 0 | 0 | 31 January 2020 | 2020-01-31 | 1 | 0 | 0 | 0 | 0 | 0 |
| 2 | 0 | 0 | 0 | 1 February 2020 | 2020-02-01 | 1 | 0 | 0 | 0 | 0 | 0 |
| 3 | 1 | 0 | 0 | 2 February 2020 | 2020-02-02 | 2 | 0 | 0 | 0 | 0 | 0 |
| 4 | 1 | 0 | 0 | 3 February 2020 | 2020-02-03 | 3 | 0 | 0 | 0 | 0 | 0 |
| 5 | 0 | 0 | 0 | 4 February 2020 | 2020-02-04 | 3 | 0 | 0 | 0 | 0 | 0 |
| 6 | 0 | 0 | 0 | 5 February 2020 | 2020-02-05 | 3 | 0 | 0 | 2 | 0 | 0 |
| 7 | 0 | 0 | 0 | 6 February 2020 | 2020-02-06 | 3 | 0 | 0 | 2 | 0 | 0 |
| 8 | 0 | 0 | 0 | 7 February 2020 | 2020-02-07 | 3 | 0 | 0 | 2 | 0 | 0 |
| 9 | 0 | 0 | 0 | 8 February 2020 | 2020-02-08 | 3 | 0 | 0 | 2 | 0 | 0 |
fig = go.Figure(go.Funnel(
x = [timeseries['totalconfirmed'].iloc[-1],timeseries['totalrecovered'].iloc[-1],
timeseries['totaldeceased'].iloc[-1]],
y = ["Total Cases", "Total Recovered", "Deaths"],
textposition = "inside",
textinfo = "value",
opacity = 0.8,
marker = {"color": [confirmed_color,recovered_color,death_color],
"line": {"width": 2.5, "color": 'Black'}},
connector = {"line": {"color": "navy", "dash": "dot", "width": 2.5}}))
fig.update_layout(
template="simple_white",
title={'text': "COVID19: Pandemic in India",'x':0.5,'y':0.9,
'xanchor': 'center','yanchor': 'top'})
fig.update_layout(height=700)
fig.show()
temp = statewise_total[statewise_total['State/UT']!='Total']
temp = temp[temp['State/UT']!='State Unassigned']
fig = go.Figure(data=[
go.Bar(name='Death', y=temp['State/UT'], x=temp['Death'],orientation='h',marker_color=death_color),
go.Bar(name='Recovered', y=temp['State/UT'], x=temp['Recovered'],orientation='h',marker_color=recovered_color),
go.Bar(name='Confirmed', y=temp['State/UT'], x=temp['Confirmed'],orientation='h',marker_color=confirmed_color)
])
fig.update_layout(barmode='stack',title='Statewise Confirmed/Recovered/Death Stacked', xaxis_title="Cases", yaxis_title="State/UT",
yaxis_categoryorder = 'total ascending', height = 1000,
template='simple_white')
fig.show()
temp = statewise_total[statewise_total['State/UT']!='Total']
temp = temp[temp['State/UT']!='State Unassigned']
fig = go.Figure()
fig.add_trace(go.Scatter(y=temp['Death'], x=temp['State/UT'],
mode='lines+markers',
name='Death',marker_color=death_color))
fig.add_trace(go.Scatter(y=temp['Recovered'], x=temp['State/UT'],
mode='lines+markers',
name='Recovered',marker_color=recovered_color))
fig.add_trace(go.Scatter(y=temp['Active'], x=temp['State/UT'],
mode='lines+markers', name='Active',marker_color=active_color))
fig.add_trace(go.Scatter(y=temp['Confirmed'], x=temp['State/UT'],
mode='lines+markers', name='Confirmed',marker_color=confirmed_color))
fig.update_layout(height=900,width= 1200, title_text="Statewise Cases",template='simple_white')
fig.show()
temp = statewise_total[statewise_total['State/UT']!='Total']
temp = temp[temp['State/UT']!='State Unassigned']
fig = go.Figure()
fig.add_trace(go.Scatter(y=temp['MortalityRate%'], x=temp['State/UT'],
mode='lines+markers',
name='Mortality Rate',marker_color=death_color))
fig.add_trace(go.Scatter(y=temp['RecoveryRate%'], x=temp['State/UT'],
mode='lines+markers',
name='Recovery Rate',marker_color=recovered_color))
fig.add_trace(go.Scatter(y=temp['Active/100 Confirmed'], x=temp['State/UT'],
mode='lines+markers', name='Active/100 Confirmed',marker_color=active_color))
fig.update_layout(height=700,width= 1200, title_text="Statewise Cases per 100 Confirmed",template='simple_white')
fig.show()
fig = px.bar(timeseries, x='date', y='totalconfirmed', color_discrete_sequence=[confirmed_color],template='simple_white')
fig.update_layout(title='Confirmed', xaxis_title="Date", yaxis_title="No. of Confirmed Cases")
fig.add_scatter(x=timeseries['date'],y=timeseries['7dyMnConfirmed'],name='7 day mean Confirmed',
marker={'color': 'red','opacity': 0.6,'colorscale': 'Viridis'},)
fig.show()
fig = px.bar(timeseries, x='date', y='totalrecovered',
color_discrete_sequence=[recovered_color],template='simple_white')
fig.update_layout(title='Recovered', xaxis_title="Date", yaxis_title="No. of Recovered Cases")
fig.add_scatter(x=timeseries['date'],y=timeseries['7dyMnRecovered'],name='7 day mean Recovered',
marker={'color': 'red','opacity': 0.6,'colorscale': 'Viridis'},)
fig.show()
fig = px.bar(timeseries, x='date', y='totaldeceased',
color_discrete_sequence=[death_color],template='simple_white')
fig.update_layout(title='Death', xaxis_title="Date", yaxis_title="No. of Deceased Cases")
fig.add_scatter(x=timeseries['date'],y=timeseries['7dyMnDeceased'],name='7 day mean Deceased',
marker={'color': 'black','opacity': 0.6,'colorscale': 'Viridis'},)
fig.show()
timeseries['totalactive'] = timeseries.totalconfirmed-timeseries.totalrecovered-timeseries.totaldeceased
fig = px.line(color_discrete_sequence=[confirmed_color],template='simple_white')
fig.add_scatter(x=timeseries['date'],y=timeseries['totalconfirmed'],name='Total Confirmed',marker={'color': confirmed_color,'opacity': 0.6,'colorscale': 'Viridis'},)
fig.add_scatter(x=timeseries['date'],y=timeseries['totalrecovered'],name='Total Recovered',marker={'color': recovered_color,'opacity': 0.6,'colorscale': 'Viridis'},)
fig.add_scatter(x=timeseries['date'],y=timeseries['totaldeceased'],name='Total Death',marker={'color': death_color,'opacity': 0.6,'colorscale': 'Viridis'})
fig.add_scatter(x=timeseries['date'],y=timeseries['totalactive'],name='Total Active',marker={'color': active_color,'opacity': 0.6,'colorscale': 'Viridis'})
fig.update_layout(title='Total Cases', xaxis_title="Date", yaxis_title="No. of Cases")
fig.show()
temp = district_wise.sort_values('Confirmed').tail(21)
fig = go.Figure(data=[
go.Bar(name='Death', y=temp['District'], x=temp['Death'].head(21),orientation='h',marker_color=death_color),
go.Bar(name='Recovered', y=temp['District'], x=temp['Recovered'].head(21),orientation='h',marker_color=recovered_color),
go.Bar(name='Confirmed', y=temp['District'], x=temp['Confirmed'].head(21),orientation='h',marker_color=confirmed_color)
])
fig.update_layout(barmode='stack',title='Top21 Confirmed/Recovered/Death Stacked', xaxis_title="Cases", yaxis_title="District",
yaxis_categoryorder = 'total ascending',
template='simple_white')
fig.show()
Unknown is Delhi.
district_wise = district_wise[district_wise['State/UT']!='State Unassigned']
district_wise.sort_values('Confirmed', ascending= False).head(30).fillna(0).style\
.background_gradient(cmap='Blues_r',subset=["Confirmed"])\
.background_gradient(cmap='Greens_r',subset=["Recovered"])\
.background_gradient(cmap='Reds_r',subset=["Death"])\
.background_gradient(cmap='pink_r',subset=["Active"])
| State/UT | District | Confirmed | Recovered | Active | Death | |
|---|---|---|---|---|---|---|
| 160 | Delhi | Unknown | 1436101 | 1411327 | -293 | 25067 |
| 289 | Karnataka | Bengaluru Urban | 1231474 | 1207260 | 8290 | 15923 |
| 361 | Maharashtra | Pune | 1099851 | 1066447 | 14419 | 18679 |
| 351 | Maharashtra | Mumbai | 738239 | 715650 | 4212 | 15968 |
| 368 | Maharashtra | Thane | 592025 | 574998 | 5901 | 11092 |
| 619 | Tamil Nadu | Chennai | 540300 | 529907 | 2048 | 8345 |
| 353 | Maharashtra | Nagpur | 493063 | 483591 | 264 | 9137 |
| 325 | Kerala | Malappuram | 440734 | 410437 | 28759 | 1508 |
| 318 | Kerala | Ernakulam | 430396 | 401576 | 26921 | 1830 |
| 356 | Maharashtra | Nashik | 403658 | 394257 | 839 | 8561 |
| 324 | Kerala | Kozhikode | 387573 | 362390 | 23271 | 1869 |
| 329 | Kerala | Thrissur | 355009 | 341779 | 11239 | 1965 |
| 328 | Kerala | Thiruvananthapuram | 343093 | 330216 | 9386 | 3376 |
| 766 | West Bengal | North 24 Parganas | 320162 | 314358 | 1213 | 4591 |
| 762 | West Bengal | Kolkata | 311183 | 305367 | 835 | 4981 |
| 335 | Maharashtra | Ahmednagar | 294758 | 283222 | 5231 | 6304 |
| 322 | Kerala | Kollam | 284264 | 280404 | 2412 | 1389 |
| 8 | Andhra Pradesh | East Godavari | 281384 | 277087 | 3065 | 1232 |
| 326 | Kerala | Palakkad | 269048 | 249691 | 17683 | 1652 |
| 711 | Uttar Pradesh | Lucknow | 238644 | 235939 | 54 | 2651 |
| 170 | Gujarat | Ahmedabad | 238022 | 234550 | 61 | 3411 |
| 317 | Kerala | Alappuzha | 237133 | 227678 | 8265 | 1150 |
| 7 | Andhra Pradesh | Chittoor | 234198 | 229637 | 2778 | 1783 |
| 323 | Kerala | Kottayam | 231995 | 223230 | 7989 | 762 |
| 620 | Tamil Nadu | Coimbatore | 231863 | 227403 | 2259 | 2201 |
| 365 | Maharashtra | Satara | 226151 | 214414 | 6239 | 5471 |
| 349 | Maharashtra | Kolhapur | 199490 | 189411 | 4427 | 5647 |
| 320 | Kerala | Kannur | 199236 | 189191 | 8847 | 1147 |
| 367 | Maharashtra | Solapur | 194066 | 183777 | 5207 | 4986 |
| 364 | Maharashtra | Sangli | 192215 | 180025 | 6964 | 5217 |